package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.feign.AdChannelFegin;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.HotArticlesService;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.vo.HotArticleVo;
import com.heima.model.common.dtos.ResponseResult;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author feng js
 */
@Service
public class HotArticlesServiceImpl implements HotArticlesService {

    @Autowired
    private ApArticleMapper apArticleMapper;
    @Autowired
    private AdChannelFegin adChannelFegin;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public void computeHotArticle() {
        //查询五天前的所有文章
        //获取时间
        String time = DateTime.now().minusDays(5).toString("yyyy-MM-dd 00:00:00");
        //查询数据
        List<ApArticle> apArticleList = apArticleMapper.selectList(Wrappers.<ApArticle>lambdaQuery().gt(ApArticle::getPublishTime, time));

        //对文章经行打分
        List<HotArticleVo> hotArticleVoList = computeHotArticle(apArticleList);

        //将数据保存到redis中
        save(hotArticleVoList);
    }

    private void save(List<HotArticleVo> hotArticleVoList) {
        ResponseResult adAllChannel = adChannelFegin.findAll();
        List<AdChannel> adChannels = JSON.parseArray(JSON.toJSONString(adAllChannel.getData()), AdChannel.class);
        if (adChannels != null && adChannels.size() > 0) {
            for (AdChannel adChannel : adChannels) {
                List<HotArticleVo> collect = hotArticleVoList.stream().filter(x -> x.getChannelId().equals(adChannel.getId())).collect(Collectors.toList());
                saveRedis(collect,ArticleConstants.HOT_ARTICLE_FIRST_PAGE+adChannel.getId());
            }
        }
        //首页数据
        saveRedis(hotArticleVoList, ArticleConstants.HOT_ARTICLE_FIRST_PAGE + ArticleConstants.DEFAULT_TAG);


    }

    private void saveRedis(List<HotArticleVo> collect, String s) {
        collect.sort(new Comparator<HotArticleVo>() {
            @Override
            public int compare(HotArticleVo o1, HotArticleVo o2) {
                return o2.getScore()-o1.getScore();
            }
        });
        if ( collect.size() == 0) {
            redisTemplate.opsForValue().set(s,"");
            return;
        }
        if (collect.size() > 30) {
            collect = collect.subList(0, 30);
        }

        redisTemplate.opsForValue().set(s,JSON.toJSONString(collect));
    }


    private List<HotArticleVo> computeHotArticle(List<ApArticle> apArticleList) {
        List<HotArticleVo> hotArticleVos = new ArrayList<>();
        if (apArticleList != null && apArticleList.size() > 0) {
            for (ApArticle apArticle : apArticleList) {
                HotArticleVo hotArticleVo = new HotArticleVo();
                BeanUtils.copyProperties(apArticle,hotArticleVo);
                Integer sorce = computeSorce(apArticle);
                hotArticleVo.setScore(sorce);
                hotArticleVos.add(hotArticleVo);
            }
        }
        return hotArticleVos;

    }

    private Integer computeSorce(ApArticle apArticle) {

        Integer sorce = 0;
        if (apArticle.getCollection() != null) {
            sorce+=apArticle.getCollection()*ArticleConstants.HOT_ARTICLE_COLLECTION_WEIGHT;
        }
        if (apArticle.getComment() != null) {
            sorce+=apArticle.getComment()*ArticleConstants.HOT_ARTICLE_COMMENT_WEIGHT;
        }
        if (apArticle.getLikes() != null) {
            sorce+=apArticle.getLikes()*ArticleConstants.HOT_ARTICLE_LIKE_WEIGHT;
        }
        return sorce;
    }
}
